{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T12:23:35.929073Z",
     "start_time": "2018-05-10T12:23:35.854509Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:32:51.857430Z",
     "start_time": "2018-05-11T00:32:51.275959Z"
    }
   },
   "outputs": [],
   "source": [
    "from keras.applications.mobilenet import MobileNet\n",
    "from keras.preprocessing import image\n",
    "from keras.applications.mobilenet import preprocess_input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:32:55.920942Z",
     "start_time": "2018-05-11T00:32:52.873403Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model_mobilenet = MobileNet(input_shape=(224, 224, 3), include_top=False, weights='imagenet')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-05T09:23:28.682481Z",
     "start_time": "2018-05-05T09:23:05.920130Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "training_set_mobilenet_features = []\n",
    "for i in list(range(1, 401)) + list(range(501, 901)):\n",
    "    img_path = 'test8/%06d.png' % i\n",
    "    # print(img_path)\n",
    "    img = image.load_img(img_path, target_size=(224, 224))# 读取图片\n",
    "    \n",
    "    #plt.imshow(img)\n",
    "    #plt.show()# 显示图片\n",
    "    x = image.img_to_array(img)# 图片转换为ndarray\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)# 数组转换为mobilenet输入格式\n",
    "\n",
    "    training_set_mobilenet_features.append(model_mobilenet.predict(x).reshape((7*7*1024, )))# 计算该张图片的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-05T09:23:28.865539Z",
     "start_time": "2018-05-05T09:23:28.801974Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "training_set_mobilenet_features_ndarray = np.vstack(training_set_mobilenet_features)# 转换为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-05T09:23:28.989018Z",
     "start_time": "2018-05-05T09:23:28.986057Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "training_set_label = np.array([1.0 if i < 400 else 0.0 for i in range(800)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-05T09:23:33.429103Z",
     "start_time": "2018-05-05T09:23:29.118097Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "test_set_mobilenet_features = []\n",
    "for i in list(range(401, 501)) + list(range(901, 1001)):\n",
    "    img_path = 'test8/%06d.png' % i\n",
    "    # print(img_path)\n",
    "    img = image.load_img(img_path, target_size=(224, 224))# 读取图片\n",
    "    x = image.img_to_array(img)# 图片转换为ndarray\n",
    "    x = np.expand_dims(x, axis=0)\n",
    "    x = preprocess_input(x)# 数组转换为mobilenet输入格式\n",
    "\n",
    "    test_set_mobilenet_features.append(model_mobilenet.predict(x).reshape((7*7*1024, )))# 计算该张图片的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-05T09:23:33.602859Z",
     "start_time": "2018-05-05T09:23:33.585940Z"
    },
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "test_set_mobilenet_features_ndarray = np.vstack(test_set_mobilenet_features)# 转换为ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:28.872237Z",
     "start_time": "2018-05-10T10:15:28.627377Z"
    },
    "run_control": {
     "marked": true
    }
   },
   "outputs": [],
   "source": [
    "smoke_dataset_image_1 = np.load('smoke_data/set1_48_data.npy')\n",
    "smoke_dataset_label_1 = np.load('smoke_data/set1_48_label.npy')\n",
    "smoke_dataset_image_2 = np.load('smoke_data/set2_48_data.npy')\n",
    "smoke_dataset_label_2 = np.load('smoke_data/set2_48_label.npy')\n",
    "smoke_dataset_image_3 = np.load('smoke_data/set3_48_data.npy')\n",
    "smoke_dataset_label_3 = np.load('smoke_data/set3_48_label.npy')\n",
    "smoke_dataset_image_4 = np.load('smoke_data/set4_48_data.npy')\n",
    "smoke_dataset_label_4 = np.load('smoke_data/set4_48_label.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:30.553915Z",
     "start_time": "2018-05-10T10:15:29.784104Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set = np.array([cv2.resize(i, (224, 224)) for i in smoke_dataset_image_1]) * 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:43.698532Z",
     "start_time": "2018-05-10T10:15:30.717184Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_mobilenet_features_ndarray = model_mobilenet.predict(preprocess_input(training_set)).reshape((1383, 7*7*1024))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:43.881632Z",
     "start_time": "2018-05-10T10:15:43.878909Z"
    }
   },
   "outputs": [],
   "source": [
    "training_set_label = smoke_dataset_label_1.flatten().astype(np.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:44.883822Z",
     "start_time": "2018-05-10T10:15:44.062378Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set = np.array([cv2.resize(i, (224, 224)) for i in smoke_dataset_image_2]) * 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:58.732865Z",
     "start_time": "2018-05-10T10:15:45.082880Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test_set_mobilenet_features_ndarray = model_mobilenet.predict(preprocess_input(test_set)).reshape((1505, 7*7*1024))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:15:59.161872Z",
     "start_time": "2018-05-10T10:15:59.158346Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_label = smoke_dataset_label_2.flatten().astype(np.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:31:08.064400Z",
     "start_time": "2018-05-11T00:31:08.061263Z"
    }
   },
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Activation, Dropout, Dense\n",
    "from keras.utils.vis_utils import plot_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:31:11.581377Z",
     "start_time": "2018-05-11T00:31:11.452785Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector = Sequential()\n",
    "model_smoke_detector.add(Dense(128, activation='sigmoid', input_shape=(7*7*1024, )))# 加入全连接层\n",
    "model_smoke_detector.add(Dropout(0.5))# 加入dropout防止过拟合\n",
    "model_smoke_detector.add(Dense(1, activation='sigmoid'))# 加入全连接层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:31:12.039143Z",
     "start_time": "2018-05-11T00:31:12.015189Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.compile(optimizer='rmsprop',\n",
    "      loss='binary_crossentropy',\n",
    "      metrics=['accuracy'])\n",
    "# 定义神经网络损失函数等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-11T00:33:41.944820Z",
     "start_time": "2018-05-11T00:33:41.242424Z"
    }
   },
   "outputs": [],
   "source": [
    "plot_model(model_smoke_detector, to_file='model.png', show_shapes=True)\n",
    "plot_model(model_mobilenet, to_file='mobilenet.png', show_shapes=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:16:17.282077Z",
     "start_time": "2018-05-10T10:16:00.866130Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.fit(training_set_mobilenet_features_ndarray, training_set_label, nb_epoch=10, batch_size=16)\n",
    "# 拟合数据即训练神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:16:17.779288Z",
     "start_time": "2018-05-10T10:16:17.502234Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_mobilenet_with_fc_prediction = model_smoke_detector.predict(test_set_mobilenet_features_ndarray)# 对测试集进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:16:17.997197Z",
     "start_time": "2018-05-10T10:16:17.993514Z"
    }
   },
   "outputs": [],
   "source": [
    "test_set_mobilenet_with_fc_prediction[test_set_mobilenet_with_fc_prediction >= 0.5] = 1\n",
    "test_set_mobilenet_with_fc_prediction[test_set_mobilenet_with_fc_prediction < 0.5] = 0\n",
    "test_set_mobilenet_with_fc_prediction_positive = test_set_mobilenet_with_fc_prediction[test_set_label == 1]\n",
    "test_set_mobilenet_with_fc_prediction_negative = test_set_mobilenet_with_fc_prediction[test_set_label == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:16:18.287937Z",
     "start_time": "2018-05-10T10:16:18.285056Z"
    }
   },
   "outputs": [],
   "source": [
    "print('mobilenet+全连接神经网络，测试集\\n检验率： {0}, 虚警率： {1}'.format(\n",
    "    len(test_set_mobilenet_with_fc_prediction_positive[test_set_mobilenet_with_fc_prediction_positive == 1]) / len(test_set_mobilenet_with_fc_prediction_positive),\n",
    "    len(test_set_mobilenet_with_fc_prediction_negative[test_set_mobilenet_with_fc_prediction_negative == 1]) / len(test_set_mobilenet_with_fc_prediction_negative)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-10T10:16:58.361519Z",
     "start_time": "2018-05-10T10:16:58.241846Z"
    }
   },
   "outputs": [],
   "source": [
    "model_smoke_detector.save('smoke_detecctor_with_mobilenet.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {
    "height": "196px",
    "width": "194px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
